package com.lanqiao.controller;

import com.lanqiao.dao.userDAO;
import com.lanqiao.entity.userinfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/user", loadOnStartup = 2)
public class userCodeController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method.equals("bb")) {
            //实现添加功能
            bb(req, resp);
        } else if (method.equals("select")) {
            //实现添加功能
            select(req, resp);
        } else if (method.equals("aa")) {
            aa(req, resp);
        } else if (method.equals("add")) {
            add(req, resp);
        }
    }

    public void bb(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("addusers.jsp").forward(req, resp);
    }

    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        userDAO s1 = new userDAO();
        List<userinfo> userList = s1.selectUser("","","");
        //通过setAttibute方法将值传递给页面
        req.setAttribute("userList", userList);
        req.getRequestDispatcher("user.jsp").forward(req, resp);
    }

    //查询
    protected void aa(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String user_name = req.getParameter("user_name");
        String user_sex = req.getParameter("user_sex");
        String user_phone = req.getParameter("user_phone");
        userDAO s1 = new userDAO();
        List<userinfo> list = s1.selectUser(user_name, user_sex, user_phone);
        req.setAttribute("list", list);
        req.getRequestDispatcher("user.jsp").forward(req, resp);
    }

    //添加功能
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //获取表单提交的内容
        String user_name = req.getParameter("user_name");
        String user_sex = req.getParameter("user_sex");
        String user_birth = req.getParameter("user_birth");
        String user_phone = req.getParameter("user_phone");
        String user_home = req.getParameter("user_home");
        String user_type = req.getParameter("user_type");
        String user_email = req.getParameter("user_email");
        String user_pwd = req.getParameter("user_pwd");
        //添加学生信息到数据库 调用studentDAO方法
        userDAO s1 = new userDAO();
        s1.inserUser(user_name, user_sex, user_birth, user_phone, user_home, user_type, user_email, user_pwd);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        resp.getWriter().println(
                "<script>alert('添加成功');window.location.href='" + req.getContextPath() + "/user?method=select';</script>"
        );
    }

}

